import grpc
from user_srv.proto import user_pb2_grpc, user_pb2


class UserTest:
    def __init__(self):
        # 连接 grpc 服务器
        channel = grpc.insecure_channel("127.0.0.1:50051")
        self.stub = user_pb2_grpc.UserStub(channel)

    def user_list(self):
        rsp: user_pb2.UserListResponse = self.stub.GetUserList(user_pb2.PageInfo(pn=1, pSize=2))
        print(rsp.total)
        for user in rsp.data:
            print(user.mobile, user.birthday, user.nickName)

    def get_user_by_id(self, id):
        rsp: user_pb2.UserInfoResponse = self.stub.GetUserById(user_pb2.IdRequest(id=id))
        print(rsp.id, rsp.gender, rsp.nickName, rsp.birthday)

    def get_user_by_mobile(self, mobile):
        try:
            rsp: user_pb2.UserInfoResponse = self.stub.GetUserByMobile(user_pb2.MobileRequest(mobile=mobile))
            print(rsp.id, rsp.gender, rsp.nickName, rsp.birthday)
        except grpc.RpcError as e:
            print(e.code(), e.details())
            print(e.code().name)
            print(e.code().value)
            # pass


if __name__ == "__main__":
    user = UserTest()
    # user.user_list()
    # user.get_user_by_id(1)
    user.get_user_by_mobile("167909834620")
